import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.List;


/**
 * ClassName: dom4jTest
 * Package: IntelliJ IDEA
 * Description:XML文件的DOM4j解析
 * Written by HanXu
 * Create 2024.07.29 17:51
 */
public class dom4jTest {
    @Test
    public void testRead() throws Exception{
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new FileInputStream("resources/jdbc.xml"));

        /*
        //通过输入流获取xml配置文件，并使用saxReader解析为document对象
        URL resource = dom4jTest.class.getClassLoader().getResource("/jdbc.xml");
        Document document = saxReader.read(resource);
        */

        /**
         * Node 节点
         *      Element 元素节点
         *      Attribute 属性节点
         *      Text 文本节点
         */

        //获取跟标签，返回Element对象，打印标签名称
        Element rootElement = document.getRootElement();
        System.out.println(rootElement.getName());//jdbc

        List<Element> elements = rootElement.elements();
        for(Element element: elements){
            System.out.println("\t"+element.getName());

            List<Element> els = element.elements();
            for(Element eles : els){
                System.out.println("\t\t"+eles.getName()+":"+eles.getText());
            }

        }//dev test


    }
}
